import request from '@/utils/request'

// 加载字典
export function initDic(refDic) {
  let key = Object.keys(refDic.value)
  let pList = []

  key.forEach((item) => {
    if (!api[item]) {
      return
    }
    let p = api[item]().then((res) => {
      refDic.value[item] = []
      refDic.value[item] = res.data || res.rows
    })
    pList.push(p)
  })

  return Promise.all(pList)
}

export function updateDic(key, refDic, sendData = {}) {
  return api[key](sendData).then((res) => {
    // res.data.unshift({
    //   id: '',
    //   name: '全部',
    // })

    refDic.value[key] = []
    refDic.value[key] = res.data
  })
}

// 字典
const api = {
  // 院系
  collegeList(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/business/base/college/list',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.data) {
            item.label = item.collegeName
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 班级列表
  classList(sendData) {
    return request.request({
      url: '/business/base/class/list',
      method: 'GET',
      params: sendData,
    })
  },

  // 省市区地理信息
  areaList(sendData) {
    return request.request({
      url: '/common/area/all',
      method: 'GET',
      params: sendData,
    })
  },

  // 部门
  deptList(sendData) {
    return request.request({
      url: '/business/base/dept/list',
      method: 'GET',
      params: sendData,
    })
  },

  // 网关型号
  gatewayTypeList(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/ammeter/device/gateway/type/list',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.data) {
            item.label = item.name
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 网关列表
  gatewayList() {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/ammeter/device/gateway/list',
        method: 'GET',
        params: {
          pageSize: 1000,
        },
      }
      request
        .request(opt)
        .then((res) => {
          res.data = res.rows
          for (let item of res.data) {
            item.label = item.name
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 电表型号
  ammeterList(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/ammeter/device/ammeter/type/list',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.data) {
            item.label = item.name
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 身份策略
  standingList(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/option/standing/list',
        method: 'GET',
        params: sendData,
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.data) {
            item.label = item.standingName
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 电表型号
  ammeterTypeOption(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/ammeter/device/ammeter/type/list',
        method: 'GET',
        params: { pageSize: 100000 },
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.data) {
            item.label = item.name
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 电价方案
  electrovalenceOption(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/option/electrovalence/list',
        method: 'GET',
        params: { pageSize: 100000 },
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.rows) {
            item.label = item.electrovalenceOptionName
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 定时开合闸方案
  timingOption(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/option/timing/list',
        method: 'GET',
        params: { pageSize: 100000 },
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.rows) {
            item.label = item.timingName
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 负载控制方案
  loadOption(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/option/load/list',
        method: 'GET',
        params: { pageSize: 100000 },
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.rows) {
            item.label = item.loadOptionName
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 补助发放方案
  subsidyOption(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/option/subsidy/list',
        method: 'GET',
        params: { pageSize: 100000 },
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.rows) {
            item.label = item.subsidyOptionName
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 恶性负载方案
  malignancyOption(sendData) {
    return new Promise((resolve, reject) => {
      const opt = {
        url: '/option/malignancy/list',
        method: 'GET',
        params: { pageSize: 100000 },
      }
      request
        .request(opt)
        .then((res) => {
          for (let item of res.rows) {
            item.label = item.malignancyName
            item.value = item.id
          }
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
}

export default api
export const $dic = api

// 校园建筑物tree查询 "type": 0 //节点类型 0学校 1园区 2楼宇 3单元 4楼层
export function $buildingTree(sendData) {
  return new Promise((resolve, reject) => {
    const opt = {
      url: '/base/building/tree',
      method: 'GET',
      params: sendData,
    }
    request
      .request(opt)
      .then((res) => {
        resolve(res)
      })
      .catch((error) => {
        reject(error)
      })
  })
}
